package indi.zhifa.core.common.web.config.fastjson;


import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 芝法酱
 */
@Configuration
public class FastJsonHttpMessageConverterConfig {

    @Primary
    @Bean(name = "default-fastjson-cfg")
    public FastJsonConfig getDefaultFastJsonConfig(){
        FastJsonConfig config = new FastJsonConfig();
        config.setWriterFeatures(
                // 保留 Map 空的字段
                JSONWriter.Feature.WriteMapNullValue,
                // 将 String 类型的 null 转成""
                JSONWriter.Feature.WriteNullStringAsEmpty,
                // 将 List 类型的 null 转成 []
                JSONWriter.Feature.WriteNullListAsEmpty,
                // 将 Boolean 类型的 null 转成 false
                // BigDecimal不使用科学计数法
                JSONWriter.Feature.WriteBigDecimalAsPlain,
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                // 漂亮的输出，这里是学习用，正式工程请注释掉
                JSONWriter.Feature.PrettyFormat,
                // 把Long转化为String
                JSONWriter.Feature.WriteLongAsString,
                // 把BigDecimal转化为String
                JSONWriter.Feature.BrowserCompatible,
                // 枚举
                JSONWriter.Feature.WriteEnumsUsingName,
                // 把byteArray改成base64
                JSONWriter.Feature.WriteByteArrayAsBase64,
                // 空值不再显示，避免流量浪费，请与前端商量好，多数情况下不要加这项配置
                JSONWriter.Feature.NotWriteDefaultValue);
        config.setReaderFeatures(
                JSONReader.Feature.Base64StringAsByteArray
        );
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return config;
    }

    @Bean(name = "SSE-fastjson-cfg")
    public FastJsonConfig getSSEFastJsonConfig(){
        FastJsonConfig config = new FastJsonConfig();
        config.setWriterFeatures(
                // 保留 Map 空的字段
                //JSONWriter.Feature.WriteMapNullValue,
                // 将 String 类型的 null 转成""
                //JSONWriter.Feature.WriteNullStringAsEmpty,
                // 将 List 类型的 null 转成 []
                //JSONWriter.Feature.WriteNullListAsEmpty,
                // 将 Boolean 类型的 null 转成 false
                // BigDecimal不使用科学计数法
                JSONWriter.Feature.WriteBigDecimalAsPlain,
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                // 漂亮的输出，这里是学习用，正式工程请注释掉
                //JSONWriter.Feature.PrettyFormat,
                // 把Long转化为String
                JSONWriter.Feature.WriteLongAsString,
                // 把BigDecimal转化为String
                JSONWriter.Feature.BrowserCompatible,
                // 枚举
                JSONWriter.Feature.WriteEnumsUsingName,
                // 把byteArray改成base64
                JSONWriter.Feature.WriteByteArrayAsBase64);
        config.setReaderFeatures(
                JSONReader.Feature.Base64StringAsByteArray
        );
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return config;
    }

    @Primary
    @Bean(name = "default-MessageConverter")
    public FastJsonHttpMessageConverter getFastJsonHttpMessageConverter(FastJsonConfig pFastJsonConfig){

        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setFastJsonConfig(pFastJsonConfig);
        List<MediaType> mediaTypeList = new ArrayList<>();
        // 解决中文乱码问题，相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
        mediaTypeList.add(MediaType.APPLICATION_JSON);
        //mediaTypeList.add(MediaType.TEXT_HTML);
        converter.setSupportedMediaTypes(mediaTypeList);
        return converter;
    }

    @Bean(name = "SSE-MessageConverter")
    public FastJsonHttpMessageConverter getFastJsonHttpMessageConverterForSSE(@Qualifier(value = "SSE-fastjson-cfg") FastJsonConfig pFastJsonConfig){

        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setFastJsonConfig(pFastJsonConfig);
        List<MediaType> mediaTypeList = new ArrayList<>();
        // 解决中文乱码问题，相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
        mediaTypeList.add(MediaType.TEXT_EVENT_STREAM);
        converter.setSupportedMediaTypes(mediaTypeList);
        return converter;
    }

}
